package com.txsj.scan.presenter;


import com.google.gson.Gson;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.AllocationDetailsBean;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.AllocationListIView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 冯瑞雨 on 2018/11/6.
 * 库存调拨 调拨单 p层
 */
public class AllocationListPresenter extends BasePresenter<AllocationListIView> {

    public AllocationListPresenter(AllocationListIView view) {
        super(view);
    }

    //获取调拨单详情
    public void getAllocationListDetails(int bill_id) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("bill_id", bill_id + "");//调出仓库id 默认是零
        RetrofitManager.getDefault().create(ApiServer.class).getAllocationListDetails(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<AllocationDetailsBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<AllocationDetailsBean> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData()) {
                                view.getAllocationListDetailsSuccess(result.getData());
                            } else {
                                view.getAllocationListDetailsFail("");
//                                view.onError("提交失败，请重试");
                            }
                        } else {
//                            view.onError("提交失败，请重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
//                        view.onError("网络异常，请重试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    protected void initModel() {

    }
}
